home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0493 / SEARCH.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-15  |  4KB  |  84 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 393 of 473
  3. From : Hagen Lehmann                       2:244/59.1           12 Apr 93  14:00
  4. To   : Timothy Glenn
  5. Subj : Search procedure
  6. ────────────────────────────────────────────────────────────────────────────────
  7. Hi Timothy,
  8.  
  9. > TG> Can someone help me make a search procedure that will read
  10. > TG> from a record format, from the disk!!!
  11.  
  12. The easiest way to search a record in a file is to read the records from file
  13. and compare them with the record that is to be searched.
  14.  
  15. If you simply want to search for a string then I've got something for you. ;-)
  16. Look at this function: }
  17.  
  18.         Function Search(SearchFor : String;
  19.                         FileName  : String) : LongInt;
  20.         Var F               : File;
  21.             Pos,Dummy       : LongInt;
  22.             BufSize,ReadNum : Word;
  23.             Buffer          : ^Byte;
  24.             Found           : Boolean;
  25.  
  26.          Function SearchString(Var Data;
  27.                                Size : Word;
  28.                                Str  : String) : LongInt;
  29.          Var S     : String;
  30.              Loop  : LongInt;
  31.              Found : Boolean;
  32.              L     : Byte ABSOLUTE Str;
  33.          Begin
  34.            Loop  := -1;
  35.            Found := False;
  36.            If L>0 Then   { I don't search for empty strings, I'm not crazy }
  37.             Repeat
  38.               Inc(Loop);
  39.               Move(Mem[Seg(Data):Loop],       { convert buffer into string }
  40.                    Mem[Seg(S):Ofs(S)+1],L+1);
  41.               S[0] := Char(L);
  42.               If S=Str Then Found := True;             { search for string }
  43.             Until Found Or (Loop=Size-L);
  44.            If Found Then SearchString := Loop   { that's the file position }
  45.            Else          SearchString := -1;    { I couldn't find anything }
  46.          End;
  47.  
  48.         Begin
  49.           Search := -1;
  50.           If MaxAvail>65535 Then BufSize := 65535   { check available heap }
  51.           Else                   BufSize := MaxAvail;
  52.           If (BufSize>0) And (BufSize>Length(SearchFor)) Then
  53.          Begin
  54.            GetMem(Buffer,BufSize);               { reserve heap for buffer }
  55.            Assign(F,FileName);
  56.            Reset(F,1);                                         { open file }
  57.            If IOResult=0 Then
  58.           Begin
  59.             Pos   := 0;
  60.             Found := False;
  61.             Repeat
  62.               BlockRead(F,Buffer^,BufSize,ReadNum);          { read buffer }
  63.               If ReadNum>0 Then                             { anything ok? }
  64.              Begin
  65.                Dummy := SearchString(Buffer^,ReadNum,SearchFor);
  66.                If Dummy<>-1 Then                   { string has been found }
  67.               Begin
  68.                 Found := True;                            { set found flag }
  69.                 Inc(Pos,Dummy);
  70.               End
  71.               Else
  72.               Begin
  73.                 Inc(Pos,ReadNum-Length(SearchFor));
  74.                 Seek(F,Pos);                       { set new file position }
  75.               End;
  76.              End;
  77.             Until Found Or (ReadNum<>BufSize);
  78.             If Found Then Search := Pos            { string has been found }
  79.             Else          Search := -1;         { string hasn't been found }
  80.             Close(F);
  81.           End;
  82.            Release(Buffer);                        { release reserved heap }
  83.          End;
  84.         End;